
package com.uhylab.uhuvinetest.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.github.mikephil.charting.animation.Easing;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.XAxis.XAxisPosition;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.uhylab.uhuvinetest.R;
import com.uhylab.uhuvinetest.bean.Bean;

import java.util.ArrayList;
import java.util.List;


public class ListViewBarChartActivity extends Activity implements View.OnClickListener {

    private TextView button_back;
    private ImageButton button_next;
    private TextView textview_title;
    private ListView lv_history_bar;

    //柱状图颜色集合
    public static List MY_COLORS ;
    private int BAD_COLOR = Color.rgb(244, 243, 135) ;
    private int HEALTH_COLOR = Color.rgb(105, 248, 180) ;
    private int DANGEROUS_COLOR = Color.rgb(223, 66, 136) ;

    //数据
    private ArrayList<BarData> list = new ArrayList<BarData>();

    //获取从上个页面传递进来的数据
    private List<Bean> beans ;

    //
    protected String[] mDays;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_listview_chart);

        //各种控件的实现以及处理
        setContent();

        //获取数据
        setData();

        ChartDataAdapter cda = new ChartDataAdapter(getApplicationContext(), list);

        lv_history_bar.setAdapter(cda);


    }

    //获取数据
    private void setData() {

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        beans = (List<Bean>) bundle.getSerializable("data");

        for (int i = 0; i < 11; i++) {
            list.add(generateData(i));
        }
    }

    //得到从上一个界面传递过来的数据
    private BarData generateData(int t) {


        mDays = new String[beans.size()];
        for (int i = 0; i < beans.size(); i++) {
            int length = beans.get(i).getDate().length();
            mDays[i] = beans.get(i).getDate().substring(0,length - 9).substring(5);
        }


        MY_COLORS = new ArrayList();

        ArrayList<BarEntry> entries = new ArrayList<BarEntry>();
        ArrayList<String> xVals = new ArrayList<String>();

        for (int i = 0; i < beans.size(); i++) {
//            int tmp = (int) (Math.random() * 4);
            float tmp = 0;
            switch (t) {
                case 0:
                    tmp = beans.get(i).getPoint0();
                    break;
                case 1:
                    tmp = beans.get(i).getPoint1();
                    break;
                case 2:
                    tmp = beans.get(i).getPoint2();
                    break;
                case 3:
                    tmp = beans.get(i).getPoint3();
                    break;
                case 4:
                    tmp = beans.get(i).getPoint4();
                    break;
                case 5:
                    tmp = beans.get(i).getPoint5();
                    break;
                case 6:
                    tmp = beans.get(i).getPoint6();
                    break;
                case 7:
                    tmp = beans.get(i).getPoint7();
                    break;
                case 8:
                    tmp = beans.get(i).getPoint8();
                    break;
                case 9:
                    tmp = beans.get(i).getPoint9();
                    break;
                case 10:
                    tmp = beans.get(i).getPoint10();
                    break;
                default:
                    break;
            }


            entries.add(new BarEntry(tmp, i));
            int length = beans.get(i).getDate().length();
            xVals.add(beans.get(i).getDate().substring(5, length - 9));
            if (tmp == 1) {
                MY_COLORS.add(HEALTH_COLOR);
            } else if (tmp == 2) {
                MY_COLORS.add(BAD_COLOR);
            } else if (tmp == 3) {
                MY_COLORS.add(DANGEROUS_COLOR);
            } else {
                MY_COLORS.add(HEALTH_COLOR);
            }

        }

//        BarDataSet d = new BarDataSet(entries, "New DataSet " + cnt);
        BarDataSet d = new BarDataSet(entries, "New DataSet ");
        d.setBarSpacePercent(20f);
//        d.setColors(MY_COLORS);
        d.setBarShadowColor(Color.rgb(203, 203, 203));
        d.setColors(MY_COLORS);
        d.setDrawValues(false);

        ArrayList<IBarDataSet> sets = new ArrayList<IBarDataSet>();
        sets.add(d);

        BarData cd = new BarData(xVals, d);
        return cd;
    }

    //各种控件的实现以及处理
    private void setContent() {
        lv_history_bar = (ListView) findViewById(R.id.lv_history_bar);
        button_back = (TextView) findViewById(R.id.button_back);
        button_next = (ImageButton) findViewById(R.id.button_next);
        textview_title = (TextView) findViewById(R.id.textview_title);

        textview_title.setText("图表");
        button_next.setVisibility(View.GONE);
        button_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_back:
                finish();
                break;
            default:
                break;
        }
    }


    //设置适配器
    private class ChartDataAdapter extends ArrayAdapter<BarData> {

        private Typeface mTf;

        public ChartDataAdapter(Context context, List<BarData> objects) {
            super(context, 0, objects);

            mTf = Typeface.createFromAsset(getAssets(), "OpenSans-Regular.ttf");
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            BarData data = getItem(position);

            ViewHolder holder = null;

            if (convertView == null) {

                holder = new ViewHolder();

                convertView = LayoutInflater.from(getContext()).inflate(
                        R.layout.list_item_barchart, null);
                holder.chart = (BarChart) convertView.findViewById(R.id.chart);
                holder.tv_list_char = (TextView) convertView.findViewById(R.id.tv_list_char);

                convertView.setTag(holder);

            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            // apply styling
            data.setValueTypeface(mTf);
            data.setValueTextColor(Color.BLACK);
            holder.chart.setDescription("");
            holder.chart.setDrawGridBackground(false);

            XAxis xAxis = holder.chart.getXAxis();
            xAxis.setPosition(XAxisPosition.BOTTOM);
            xAxis.setTypeface(mTf);
            xAxis.setDrawGridLines(false);

            YAxis leftAxis = holder.chart.getAxisLeft();
            leftAxis.setTypeface(mTf);
            leftAxis.setLabelCount(5, false);
            leftAxis.setSpaceTop(15f);

            YAxis rightAxis = holder.chart.getAxisRight();
        //    rightAxis.setTypeface(mTf);
            rightAxis.setLabelCount(5, false);
            rightAxis.setSpaceTop(15f);

            // set data
            holder.chart.setData(data);
            holder.chart.getData().setHighlightEnabled(false);

            // do not forget to refresh the chart
//            holder.chart.invalidate();
            holder.chart.animateY(700, Easing.EasingOption.EaseInCubic);
           // holder.chart.setEnabled(false);
            holder.chart.getLegend().setEnabled(false);
            holder.chart.setMaxVisibleValueCount(3);
            //holder.chart.setMinimumHeight(3);

            switch (position) {
                case 0:
                    holder.tv_list_char.setText("白细胞");
                    break;
                case 1:
                    holder.tv_list_char.setText("亚硝酸盐");
                    break;
                case 2:
                    holder.tv_list_char.setText("尿胆原");
                    break;
                case 3:
                    holder.tv_list_char.setText("蛋白质");
                    break;
                case 4:
                    holder.tv_list_char.setText("酸碱度");
                    break;
                case 5:
                    holder.tv_list_char.setText("潜血");
                    break;
                case 6:
                    holder.tv_list_char.setText("比重");
                    break;
                case 7:
                    holder.tv_list_char.setText("酮体");
                    break;
                case 8:
                    holder.tv_list_char.setText("胆红素");
                    break;
                case 9:
                    holder.tv_list_char.setText("葡萄糖");
                    break;
                case 10:
                    holder.tv_list_char.setText("抗坏血酸");
                    break;
                default:
                    break;
            }

            return convertView;
        }

        private class ViewHolder {

            BarChart chart;
            TextView tv_list_char;
        }
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
        overridePendingTransition(R.anim.move_left_in_activity, R.anim.move_right_out_activity);
    }

}
